package com.tongzhou.tongzhou.initInfo.dao.impl;

import java.util.List;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.tongzhou.db.DBUtil;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.tongzhou.initInfo.dao.IInitInfoDAO;
import com.tongzhou.tongzhou.initInfo.model.InitInfo;
import com.tongzhou.tongzhou.initInfo.model.QuotType;
import com.tongzhou.tongzhou.initInfo.model.Region;

public class InitInfoDAO implements IInitInfoDAO{
	@Override
	public InitInfo getInitInfoById(int id) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select id,server_call,offer_call,about_call,update_time ");
		sql.append(" from init_info ");
		sql.append(" where id='"+id+"'");
		InitInfo initInfo=DBUtil.query(sql.toString(), new BeanHandler<InitInfo>(InitInfo.class));
		if(initInfo==null){
			sql.delete(0, sql.length());
			sql.append("select id,server_call,offer_call,about_call,update_time ");
			sql.append(" from init_info ");
			sql.append(" order by id desc ");
			List<InitInfo> result=DBUtil.query(sql.toString(),new BeanListHandler<InitInfo>(InitInfo.class));
			initInfo=result.get(0);
		}
		return initInfo;
	}

	@Override
	public List<QuotType> getQuotTypeList() throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select id,quot_type_name from quot_type ");
		List<QuotType> result=DBUtil.query(sql.toString(),new BeanListHandler<QuotType>(QuotType.class));
		return result;
	}

	@Override
	public List<Region> getRegionList() throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select id,region_name from region ");
		List<Region> result=DBUtil.query(sql.toString(),new BeanListHandler<Region>(Region.class));
		return result;
	}
}
